{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ResponseId</th>\n",
       "      <th>MainBranch</th>\n",
       "      <th>Employment</th>\n",
       "      <th>Country</th>\n",
       "      <th>US_State</th>\n",
       "      <th>UK_Country</th>\n",
       "      <th>EdLevel</th>\n",
       "      <th>Age1stCode</th>\n",
       "      <th>LearnCode</th>\n",
       "      <th>YearsCode</th>\n",
       "      <th>...</th>\n",
       "      <th>Age</th>\n",
       "      <th>Gender</th>\n",
       "      <th>Trans</th>\n",
       "      <th>Sexuality</th>\n",
       "      <th>Ethnicity</th>\n",
       "      <th>Accessibility</th>\n",
       "      <th>MentalHealth</th>\n",
       "      <th>SurveyLength</th>\n",
       "      <th>SurveyEase</th>\n",
       "      <th>ConvertedCompYearly</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>I am a developer by profession</td>\n",
       "      <td>Independent contractor, freelancer, or self-em...</td>\n",
       "      <td>Slovakia</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Secondary school (e.g. American high school, G...</td>\n",
       "      <td>18 - 24 years</td>\n",
       "      <td>Coding Bootcamp;Other online resources (ex: vi...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>25-34 years old</td>\n",
       "      <td>Man</td>\n",
       "      <td>No</td>\n",
       "      <td>Straight / Heterosexual</td>\n",
       "      <td>White or of European descent</td>\n",
       "      <td>None of the above</td>\n",
       "      <td>None of the above</td>\n",
       "      <td>Appropriate in length</td>\n",
       "      <td>Easy</td>\n",
       "      <td>62268.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>I am a student who is learning to code</td>\n",
       "      <td>Student, full-time</td>\n",
       "      <td>Netherlands</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Bachelor’s degree (B.A., B.S., B.Eng., etc.)</td>\n",
       "      <td>11 - 17 years</td>\n",
       "      <td>Other online resources (ex: videos, blogs, etc...</td>\n",
       "      <td>7</td>\n",
       "      <td>...</td>\n",
       "      <td>18-24 years old</td>\n",
       "      <td>Man</td>\n",
       "      <td>No</td>\n",
       "      <td>Straight / Heterosexual</td>\n",
       "      <td>White or of European descent</td>\n",
       "      <td>None of the above</td>\n",
       "      <td>None of the above</td>\n",
       "      <td>Appropriate in length</td>\n",
       "      <td>Easy</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>I am not primarily a developer, but I write co...</td>\n",
       "      <td>Student, full-time</td>\n",
       "      <td>Russian Federation</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Bachelor’s degree (B.A., B.S., B.Eng., etc.)</td>\n",
       "      <td>11 - 17 years</td>\n",
       "      <td>Other online resources (ex: videos, blogs, etc...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>18-24 years old</td>\n",
       "      <td>Man</td>\n",
       "      <td>No</td>\n",
       "      <td>Prefer not to say</td>\n",
       "      <td>Prefer not to say</td>\n",
       "      <td>None of the above</td>\n",
       "      <td>None of the above</td>\n",
       "      <td>Appropriate in length</td>\n",
       "      <td>Easy</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>I am a developer by profession</td>\n",
       "      <td>Employed full-time</td>\n",
       "      <td>Austria</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Master’s degree (M.A., M.S., M.Eng., MBA, etc.)</td>\n",
       "      <td>11 - 17 years</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>35-44 years old</td>\n",
       "      <td>Man</td>\n",
       "      <td>No</td>\n",
       "      <td>Straight / Heterosexual</td>\n",
       "      <td>White or of European descent</td>\n",
       "      <td>I am deaf / hard of hearing</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Appropriate in length</td>\n",
       "      <td>Neither easy nor difficult</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>I am a developer by profession</td>\n",
       "      <td>Independent contractor, freelancer, or self-em...</td>\n",
       "      <td>United Kingdom of Great Britain and Northern I...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>England</td>\n",
       "      <td>Master’s degree (M.A., M.S., M.Eng., MBA, etc.)</td>\n",
       "      <td>5 - 10 years</td>\n",
       "      <td>Friend or family member</td>\n",
       "      <td>17</td>\n",
       "      <td>...</td>\n",
       "      <td>25-34 years old</td>\n",
       "      <td>Man</td>\n",
       "      <td>No</td>\n",
       "      <td>NaN</td>\n",
       "      <td>White or of European descent</td>\n",
       "      <td>None of the above</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Appropriate in length</td>\n",
       "      <td>Easy</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 48 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   ResponseId                                         MainBranch  \\\n",
       "0           1                     I am a developer by profession   \n",
       "1           2             I am a student who is learning to code   \n",
       "2           3  I am not primarily a developer, but I write co...   \n",
       "3           4                     I am a developer by profession   \n",
       "4           5                     I am a developer by profession   \n",
       "\n",
       "                                          Employment  \\\n",
       "0  Independent contractor, freelancer, or self-em...   \n",
       "1                                 Student, full-time   \n",
       "2                                 Student, full-time   \n",
       "3                                 Employed full-time   \n",
       "4  Independent contractor, freelancer, or self-em...   \n",
       "\n",
       "                                             Country US_State UK_Country  \\\n",
       "0                                           Slovakia      NaN        NaN   \n",
       "1                                        Netherlands      NaN        NaN   \n",
       "2                                 Russian Federation      NaN        NaN   \n",
       "3                                            Austria      NaN        NaN   \n",
       "4  United Kingdom of Great Britain and Northern I...      NaN    England   \n",
       "\n",
       "                                             EdLevel     Age1stCode  \\\n",
       "0  Secondary school (e.g. American high school, G...  18 - 24 years   \n",
       "1       Bachelor’s degree (B.A., B.S., B.Eng., etc.)  11 - 17 years   \n",
       "2       Bachelor’s degree (B.A., B.S., B.Eng., etc.)  11 - 17 years   \n",
       "3    Master’s degree (M.A., M.S., M.Eng., MBA, etc.)  11 - 17 years   \n",
       "4    Master’s degree (M.A., M.S., M.Eng., MBA, etc.)   5 - 10 years   \n",
       "\n",
       "                                           LearnCode YearsCode  ...  \\\n",
       "0  Coding Bootcamp;Other online resources (ex: vi...       NaN  ...   \n",
       "1  Other online resources (ex: videos, blogs, etc...         7  ...   \n",
       "2  Other online resources (ex: videos, blogs, etc...       NaN  ...   \n",
       "3                                                NaN       NaN  ...   \n",
       "4                            Friend or family member        17  ...   \n",
       "\n",
       "               Age Gender Trans                Sexuality  \\\n",
       "0  25-34 years old    Man    No  Straight / Heterosexual   \n",
       "1  18-24 years old    Man    No  Straight / Heterosexual   \n",
       "2  18-24 years old    Man    No        Prefer not to say   \n",
       "3  35-44 years old    Man    No  Straight / Heterosexual   \n",
       "4  25-34 years old    Man    No                      NaN   \n",
       "\n",
       "                      Ethnicity                Accessibility  \\\n",
       "0  White or of European descent            None of the above   \n",
       "1  White or of European descent            None of the above   \n",
       "2             Prefer not to say            None of the above   \n",
       "3  White or of European descent  I am deaf / hard of hearing   \n",
       "4  White or of European descent            None of the above   \n",
       "\n",
       "        MentalHealth           SurveyLength                  SurveyEase  \\\n",
       "0  None of the above  Appropriate in length                        Easy   \n",
       "1  None of the above  Appropriate in length                        Easy   \n",
       "2  None of the above  Appropriate in length                        Easy   \n",
       "3                NaN  Appropriate in length  Neither easy nor difficult   \n",
       "4                NaN  Appropriate in length                        Easy   \n",
       "\n",
       "  ConvertedCompYearly  \n",
       "0             62268.0  \n",
       "1                 NaN  \n",
       "2                 NaN  \n",
       "3                 NaN  \n",
       "4                 NaN  \n",
       "\n",
       "[5 rows x 48 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Open the file `so_2021_survey_results.csv`, and read it into a data frame\n",
    "filename = '../data/so_2021_survey_results.csv'\n",
    "df = pd.read_csv(filename, low_memory=False)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 1\n",
    "\n",
    "When developers are stuck (as indicated in the column `NEWStuck`), what are the three things they're most likely to do?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "NEWStuck\n",
       "Google it                            74491\n",
       "Visit Stack Overflow                 66410\n",
       "Do other work and come back later    39871\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(\n",
    "    df['NEWStuck']\n",
    "    .str.split(';')\n",
    "    .explode()\n",
    "    .value_counts()\n",
    "    .head(3)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 2\n",
    "\n",
    "What proportion of the survey respondents marked their gender as `Man`?  Does that proportion seem similar to your real-life experiences?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Gender                                                                            \n",
       "Man                                                                                   0.909231\n",
       "Woman                                                                                 0.050069\n",
       "Prefer not to say                                                                     0.017524\n",
       "Non-binary, genderqueer, or gender non-conforming                                     0.008385\n",
       "Or, in your own words:                                                                0.005019\n",
       "Man;Or, in your own words:                                                            0.003257\n",
       "Man;Non-binary, genderqueer, or gender non-conforming                                 0.003062\n",
       "Woman;Non-binary, genderqueer, or gender non-conforming                               0.001786\n",
       "Man;Woman                                                                             0.000498\n",
       "Man;Woman;Non-binary, genderqueer, or gender non-conforming                           0.000255\n",
       "Non-binary, genderqueer, or gender non-conforming;Or, in your own words:              0.000255\n",
       "Man;Woman;Non-binary, genderqueer, or gender non-conforming;Or, in your own words:    0.000207\n",
       "Woman;Or, in your own words:                                                          0.000194\n",
       "Man;Non-binary, genderqueer, or gender non-conforming;Or, in your own words:          0.000134\n",
       "Woman;Non-binary, genderqueer, or gender non-conforming;Or, in your own words:        0.000109\n",
       "Man;Woman;Or, in your own words:                                                      0.000012\n",
       "Name: proportion, dtype: float64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 90% men is indeed pretty close to what I've seen in real life.\n",
    "(\n",
    "    df[['Gender']]\n",
    "    .value_counts(normalize=True)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 3\n",
    "\n",
    "On average, what proportion of their years coding have been done professionally?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5923711657118932"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[df['YearsCode'] == 'Less than 1 year', 'YearsCode'] = 0\n",
    "df.loc[df['YearsCode'] == 'More than 50 years', 'YearsCode'] = 51\n",
    "\n",
    "df.loc[df['YearsCodePro'] == 'Less than 1 year', 'YearsCodePro'] = 0\n",
    "df.loc[df['YearsCodePro'] == 'More than 50 years', 'YearsCodePro'] = 51\n",
    "\n",
    "# Turn them into integers\n",
    "df = df[['YearsCode', 'YearsCodePro']].dropna()\n",
    "df['YearsCode'] = df['YearsCode'].astype(np.int16)\n",
    "df['YearsCodePro'] = df['YearsCodePro'].astype(np.int16)\n",
    "\n",
    "# Get rid of rows with 0 YearsCode\n",
    "df = df[df['YearsCode'] != 0]\n",
    "\n",
    "(df['YearsCodePro'] / df['YearsCode']).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
